cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        _zimo:null,
        _jushuxuanze:null,
        _type:null,
        _ismai:null,
        diamondNumb:cc.Label,
    },

    // use this for initialization
    onLoad: function () {
        
        this._zimo = [];
        var t = this.node.getChildByName("zimojiacheng");//底分按钮
        for(var i = 0; i < t.childrenCount; ++i){
            var n = t.children[i].getComponent("RadioButton");
            if(n != null){
                this._zimo.push(n);
            }
        }
        
        this._jushuxuanze = [];//局数选择
        var t = this.node.getChildByName("xuanzejushu");
        for(var i = 0; i < t.childrenCount; ++i){
            var n = t.children[i].getComponent("RadioButton");
            if(n != null){
                this._jushuxuanze.push(n);
            }
        }

        this._type=[];
        var people=this.node.getChildByName("people");
        // cc.log("people.childrenCount"+people.childrenCount);
        for(var i=0;i<people.childrenCount;i++){
            var child=people.children[i].getComponent(cc.Toggle);
            if(child){
                cc.log("aaaaaCreateRoom","i="+i);
                cc.log("isChecked="+child.isChecked);
                this._type.push(child);
                // cc.log("this._type.length="+this._type.length);
            }
        }
        
        this._ismai=[];
        var ismai=this.node.getChildByName("ismai");
        // cc.log("ismai",ismai);
        for(var i=0;i<ismai.childrenCount;i++){
            var child=ismai.children[i].getComponent(cc.Toggle);
            if(child){
                cc.log("CreateRoom","i="+i);
                cc.log("isChecked="+child.isChecked);
                this._ismai.push(child);
                cc.log("this._ismai.length="+this._ismai[i].isChecked);
            }
        }
        var btn_back = cc.find("Canvas/CreateRoom/btn_back");
        cc.vv.utils.addClickEvent(btn_back,this.node,"CreateRoom","onBtnBack",3);
        var btn_ok = cc.find("Canvas/CreateRoom/btn_ok");
        cc.vv.utils.addClickEvent(btn_ok,this.node,"CreateRoom","onBtnOK",1);
    },
    
    onBtnBack:function(){
        this.node.active = false;
    },
    
    onBtnOK:function(){
        this.node.active = false;
        this.createRoom();
    },
    
    createRoom:function(){
        var self = this;
        var onCreate = function(ret){
            if(ret.errcode !== 0){
                cc.vv.wc.hide();
                //console.log(ret.errmsg);
                switch(ret.errcode){
                    case 2222:cc.vv.alert.show("提示","房卡不足，创建房间失败!"); break;
                    case 101:
                    case 102:cc.vv.alert.show("提示","由于网络或其它原因，游戏服务器连接已断开，请重试!");break;
                    case -1:
                    case 103:cc.vv.alert.show("提示","网络繁忙，请重试!");break;
                    default:cc.vv.alert.show("提示","创建房间失败,错误码:" + ret.errcode);break;
                }
                // if(ret.errcode == 2222){
                //     cc.vv.alert.show("提示","房卡不足，创建房间失败!");  
                // }
                // else{
                //     cc.vv.alert.show("提示","创建房间失败,错误码:" + ret.errcode);
                // }
            }
            else{
                // console.log("connectGameServer of createRoom.js");
                // cc.log("createRoom----------------",ret);
                cc.vv.userMgr.roomData_reconnect=ret;
                cc.vv.userMgr.roomData=ret;
                cc.log("ret........................",ret);
                cc.vv.gameNetMgr.connectGameServer(ret);
            }
        };
        
        var difen = 0;
        
        var zimo = 0;
        var dishu = 0
        for(var i = 0; i < self._zimo.length; ++i)
        {
            //底数按钮 1--【2,4,6,8】  2--【3,5,7,9】
            if(self._zimo[i].checked)
            {
                zimo = i+1;
                dishu = i+1;
                cc.vv.gameNetMgr.dishu = dishu;
                break;
            }  
               
        }

        var huansanzhang = 0;/*self._wanfaxuanze[0].checked*/       
        var jiangdui =0; /*self._wanfaxuanze[1].checked*/
        var menqing =0;// self._wanfaxuanze[2].checked;
        var tiandihu =0 //self._wanfaxuanze[3].checked;
        
        var type = 0;
        
        var len=this._type.length;
        for(var i=0;i<len;i++){
            if(this._type[i].isChecked){
                type=i;
                break;
            }
        }
        if(type == 1){
            type = "wzsr";//四人
        }
        else{
            type = "wzer";//二人
        }
        cc.log("type="+type);
        
        var ismaidi=0;
        len=this._ismai.length;
        cc.log();
        // for(var i=0;i<len;i++){
        //     if(this._ismai[i].isChecked){
        //         ismaidi=i;
        //         break;
        //     }
        // }
        // if(ismaidi==0){
        //     ismaidi=1;
        // }
        // if(ismaidi==1){
        //     ismaidi=0;
        // }
        ismaidi=this._ismai[0].isChecked==true?1:0;
        cc.log("ismaidi=",ismaidi);
        
        var zuidafanshu = 0;
        var jushuxuanze = 0;
        for(var i = 0; i < self._jushuxuanze.length; ++i){
            if(self._jushuxuanze[i].checked){
                jushuxuanze = i;
                console.log("jushuxuanze is " +jushuxuanze);
                break;
            }     
        }
        
        var dianganghua = 0;
        
        console.log("zimo="+zimo);
        var conf = {
            type:type,
            difen:difen, 
            zimo:zimo,
            dishu:dishu,
            jiangdui:jiangdui,
            huansanzhang:huansanzhang,
            zuidafanshu:zuidafanshu,
            jushuxuanze:jushuxuanze,
            dianganghua:dianganghua,
            menqing:menqing,
            tiandihu:tiandihu,
            isNeedOperDi:ismaidi,
        }; 
        
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign,
            conf:JSON.stringify(conf)
        };
        console.log(data);
        cc.vv.wc.show("正在创建房间");
        cc.vv.http.sendRequest("/create_private_room",data,onCreate);   
    },

    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        
        if(this._jushuxuanze){
            this.diamondNumb.string=this._jushuxuanze[0].checked==true?"2":(this._jushuxuanze[1].checked==true?"3":"");
            var button_4ju = cc.find("Canvas/CreateRoom/xuanzejushu/button_4ju").active = !this._jushuxuanze[0].checked;
            var button_8ju = cc.find("Canvas/CreateRoom/xuanzejushu/button_8ju").active = !this._jushuxuanze[1].checked;
        }
        if(this._zimo)
        {
           var shuangshu = cc.find("Canvas/CreateRoom/zimojiacheng/shuangshu").active = !this._zimo[0].checked;
           var danshu = cc.find("Canvas/CreateRoom/zimojiacheng/danshu").active = !this._zimo[1].checked; 
        }
        if(this._ismai){
            // cc.log("1"+this._ismai[0].isChecked,"2"+this._ismai[1].isChecked)
        }
    },
});
